import 'package:flutter/material.dart';
import 'package:flutter_base_plugin/flutter_base_plugin.dart';
import 'package:flutter_base_plugin_example/page/home/vm/home_view_model.dart';
import 'package:flutter_base_plugin_example/page/main/vm/main_view_model.dart';

import 'model/model.dart';

/// @author:  JoeKai
/// @date 2022/6/26 14:40
/// @explain：
class HomeNavBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    HomeViewModel viewModel = context.watch<HomeViewModel>();
    List<NavBar> navBarList = viewModel.navBarList;
    if (navBarList.length > 10) {
      navBarList.removeRange(10, navBarList.length);
    }
    int tempIndex = -1;
    return Container(
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular((10.r))),
        margin: EdgeInsets.only(left: 15.h, right: 15.h),
        height: 250.h,
        child: GridView.count(
          physics: const NeverScrollableScrollPhysics(), //禁止滚动
          crossAxisCount: 5,
          padding: EdgeInsets.all(5.h),
          children: navBarList.map((e) {
            tempIndex++;
            return _gridViewItem(context, e, tempIndex);
          }).toList(),
        ));
  }

  Widget _gridViewItem(BuildContext context, NavBar item, index) {
    return InkWell(
      onTap: () {
        //跳转
        if (item.path.isEmpty) {
          context.read<MainViewModel>().changeIndex(1);
        } else {
          ToastUtils.show(item.name);
        }
      },
      child: Column(
        children: <Widget>[
          SizedBox(
            height: 8.h,
          ),
          Image.network(
            "${item.image}",
            width: 70.h,
            height: 70.h,
            fit: BoxFit.fill,
          ),
          SizedBox(
            height: 4.h,
          ),
          Text(item.name)
        ],
      ),
    );
  }
}
